<?php
$ws = new Swoole\WebSocket\Server("0.0.0.0", 8090);
//监听WebSocket连接打开事件
$ws->on('open', function ($ws, $request) {
    while(1){
        $time = date("Y-m-d H:i:s");
        $ws->push($request->fd, "现在时间是： {$time}\n");
        Swoole\Coroutine::sleep(10);
    }
});

//监听WebSocket消息事件
$ws->on('Message', function ($ws, $frame) {
    sleep(1);
    $ws->push($frame->fd, "你传来的消息是: {$frame->data}");
});

//监听WebSocket连接关闭事件
$ws->on('Close', function ($ws, $fd) {
    echo "client-{$fd} is closed\n";
});

$ws->start();